package cn.yousuowei.huanglele.scanmine;

import android.util.Log;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Created by ihuanglele on 2017/4/15.
 */
public class Mine {

    private int[][] boxes;
    private int col;    // 列数，行数
    private int row;
    private int bom;        // 炸弹数量

    /**
     *  @param col int 列数
     * @param row int 行数
     * @param bom int 雷数
     */
    public Mine(int col, int row, int bom) {
        if(col * row < bom){
            // 抛出异常  不会写空着
        }else {
            boxes = new int[row][col];
            this.col = col;
            this.row = row;
            this.bom = bom;
            this.placeBom();
            this.placeOtherBom();
        }
    }


    /**
     * 安防炸弹
     */
    private void placeBom(){
        int num = this.col * this.row;
        int current_bom_num = 0;
        Random random = new Random();
        while (this.bom >= current_bom_num+1){
            int p = random.nextInt(num);

            Map<String,Integer> rc =  positionToRC(p);
            if(this.boxes[rc.get("row")][rc.get("col")] == 0){
                // 新添加一个雷
                current_bom_num++;
                this.boxes[rc.get("row")][rc.get("col")] = 9;       // 9表示雷
            }

        }
    }

    /**
     * 索引位置 转 坐标位置
     * @param p
     * @return
     */
    private Map<String,Integer> positionToRC(int p){
        int r = (int) Math.floor(p / this.col);
        int c = p % this.col;
        Map<String,Integer> map = new HashMap<String,Integer>();
        map.put("row",r);
        map.put("col",c);
        return  map;
    }

    /**
     * 计算其他的地区炸弹
     */
    private void placeOtherBom(){
        for (int i = 0; i < this.row;i++){
            for (int j = 0; j < this.col; j++){
                if(this.boxes[i][j] == 9){
                    continue;       // 雷
                }else {
                    int num = 0;    // 雷的数量
                    for (int ii = i-1; ii <= i+1; ii++ ){
                        for (int jj = j-1; jj <= j+1; jj++){
                            if(ii >= 0  && ii < this.row && jj >= 0 && jj < this.col){
                                if(ii == i && jj == j){       // 自己
                                    continue;
                                }
                                if(this.boxes[ii][jj] == 9){
                                    num++;
                                }
                            }
                        }
                    }
                    this.boxes[i][j] = num;
                }
            }
        }
    }

    /**
     * 判断当前位置是否安全
     * @param p
     * @return
     */
    public boolean isSafety(int p){
        return 9 == this.getPlaceNum(p);
    }

    public void printBom(){
        for (int i = 0; i < this.row;i++){
            for (int j = 0; j < this.col; j++){
                Log.i("printBom",this.boxes[i][j]+"");
            }
            Log.i("printBom-row","----------");
        }
    }


    /**
     * 返回当前位置的雷数 9 表示是雷
     * @param p
     * @return
     */
    public int getPlaceNum(int p){
        Map<String,Integer> rc = positionToRC(p);
        return this.boxes[rc.get("row")][rc.get("col")];
    }

}
